package util;

import java.util.List;

import javax.swing.UIManager;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;

import com.sz.sidi.entity.TmenuMain;
import com.sz.sidi.entity.TuserInfo;
import com.sz.sidi.exceptions.BusinessException;
import com.sz.sidi.oa.Activator;

public class Session {
	public Logger logger = Logger.getLogger(getClass());
	
	public static final int				initCount				= 100;	
	public static TuserInfo userinfo = null;
	
	
	/**
	 * 装载系统菜单E,F
	 * @return
	 * @throws BusinessException
	 */
	public static List<TmenuMain> loadSystemMenu(Integer type,String param)
			throws BusinessException {
		List<TmenuMain> menu = null;
		if (type == 1) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(" where FMENUTYPE ='E' order by FXH");
			menu = DataSource.oasidiService.getEnity(TmenuMain.class,
					"tmenumain", buffer.toString(), null);
		} else if (type == 2 && param != null) {
			StringBuffer buffer_1 = new StringBuffer();
			buffer_1.append(" where FMENUTYPE ='F' and fmenuparent = '" + param
					+ "' order by FXH");
			menu = DataSource.oasidiService.getEnity(TmenuMain.class,
					"tmenumain", buffer_1.toString(), null);
		}
		return menu;
	}
	
	/**
	 * 验证用户名和密码是否正确
	 * @return
	 */
	public static TuserInfo checkUser(String username, String psw) {
		TuserInfo userInfo = null;
		try {
			StringBuffer buffer = new StringBuffer();
			buffer.append(" where FGH = '" + username + "' and FPASSWORD = '"
					+ psw + "'");
			List<TuserInfo> user = DataSource.oasidiService.getEnity(
					TuserInfo.class, "TUSERINFO", buffer.toString(), null);
			if (user != null && user.size() == 1) {
				userInfo = user.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userInfo;
	}
	
	
	public static int getInitCount()
	{
		return initCount;
	}
	
	/**
	 * 初始化系统
	 */
	public static String getInitTitle(int i)
	{
		String title = "正在初始化OA......"; //$NON-NLS-1$
		try
		{
			switch (i)
			{
			case 0:
				title = "正在加载系统参数......"; //$NON-NLS-1$
				break;
			}
		} catch (Exception e)
		{
			title = ""; //$NON-NLS-1$
			showExceptionMsg(e,"");
		}
		return title;
	}
	
	/**
	 * 初始化系统
	 */
	public static boolean initSystem(int i)
	{
		boolean result = false;
		try
		{
			switch (i)
			{
			case 0:
				UIManager.setLookAndFeel(UIManager
						.getSystemLookAndFeelClassName());

				break;
			}
			result = true;
		} catch (Exception e)
		{
			result = false;
			showExceptionMsg(e,"");
			return result;
		}
		return result;
	}
	

	public static TuserInfo getUserinfo() {
		return userinfo;
	}

	public static void setUserinfo(TuserInfo Userinfo) {
		 Session.userinfo = Userinfo;
	}
	
	/**
	 * 显示异常信息
	 * 
	 * @param e
	 */
	public static void showExceptionMsg(Exception e, String s)
	{
		Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "ljs test,ERROR-: " + s, 	e));
		
		e.printStackTrace();

		if (e instanceof BusinessException)
		{
			BusinessException be = (BusinessException) e;
			if (be.getErrcode() == 1)
			{
				MessageDialog.openError(new Shell(), "系统错误", s + "\n  " //$NON-NLS-2$ //$NON-NLS-1$
						+ e.getMessage());
			} else if (be.getErrcode() == 2)
			{
				MessageDialog.openWarning(new Shell(), "系统警告", s + "\n  " //$NON-NLS-2$ //$NON-NLS-1$
						+ e.getMessage());
			} else if (be.getErrcode() == 3)
			{
				MessageDialog.openInformation(new Shell(), "系统提示", s + "\n  " //$NON-NLS-2$ //$NON-NLS-1$
						+ e.getMessage());
			} else
			{
				MessageDialog.openError(new Shell(), "系统错误", s + "\n  " //$NON-NLS-2$ //$NON-NLS-1$
						+ e.getMessage());
			}
		} else
		{
			if (s == null || s.trim().equals("")) { //$NON-NLS-1$
				s = e.getLocalizedMessage();
			}
			if (e.getMessage() != null
					&& s.trim().equals(e.getMessage().trim()))
			{
				MessageDialog.openError(new Shell(), "系统错误", s); //$NON-NLS-1$
			} else
			{
				MessageDialog.openError(new Shell(), "系统错误", s + "\n  " //$NON-NLS-2$ //$NON-NLS-1$
						+ (e.getMessage() == null ? "" : e.getMessage())); //$NON-NLS-1$
			}
		}
	}
}
